package com.jdn;

import java.io.File;
import java.util.List;

/**
 * User: arsentyev
 * Date: 22.08.12
 */
public class HtmlBuilder {
    public static String buildHtml(Container con) {
        String html = Template.TEMPLATE;

        String path = getPath(con.getPackageName());

        html = html.replace("{className}", con.getClassName()).replace("{since}", con.getSince())
                   .replace("{summary}", con.getSummary()).replace("{package}", con.getPackageName())
                   .replace("{inheritanceTree}", getTree(con.getHierarchy(), path)).replace("{authors}", getAuthors(con.getAuthors()))
                   .replace("{syntax}", getSyntax(con.getSyntax()));

        if(con.getConstructors().isEmpty()) {
            html = html.replace("{constructorDisplay}", NONE);
        } else {
            html = html.replace("<!--constructors-->", getMethods(con.getConstructors())).replace("{constructorDisplay}", BLOCK);
        }

        if(con.getFields().isEmpty()) {
            html = html.replace("{fieldDisplay}", NONE);
        } else {
            html = html.replace("<!--fields-->", getFields(con.getFields(), path)).replace("{fieldDisplay}", BLOCK);
        }

        if(con.getMethods().isEmpty()) {
            html = html.replace("{methodDisplay}", NONE);
        } else {
            html = html.replace("<!--methods-->", getMethods(con.getMethods())).replace("{methodDisplay}", BLOCK);
        }

        html = html.replace("{path}", path + "web/");

        return html;
    }

    private static String getFields(List<Container.FieldWrapper> fields, String path) {
        StringBuilder builder = new StringBuilder("");

        for(Container.FieldWrapper f: fields) {
            builder.append("<tr>");
            if("public".equals(f.mod)) {
                builder.append(PUB_F);
            } else if("private".equals(f.mod)) {
                builder.append(PRIV_F);
            } else {
                builder.append(PROT_F);
            }
            builder.append("<td><a href=\"").append(path).append(f.type.replace(".", File.separator)).append("\">").append(f.type.replaceAll("[a-z]+\\.", "")).append("</a></td>")
                   .append("<td>").append(f.name).append("</td>")
                   .append("<td><span>").append(f.desc).append("</span></td>")
                   .append("<tr>");
        }

        return builder.toString();
    }

    private static String getMethods(List<Container.Wrapper> methods) {
        StringBuilder builder = new StringBuilder("");

        for(Container.Wrapper m: methods) {
            builder.append("<tr>");
            if("public".equals(m.mod)) {
                builder.append(PUB_MET);
            } else if("private".equals(m.mod)) {
                builder.append(PRIV_MET);
            } else {
                builder.append(PROT_MET);
            }
            builder.append("<td><a href=\"#\">").append(m.name).append("</a></td>")
                    .append("<td><span>").append(m.desc).append("</span></td>")
                    .append("<tr>");
        }

        return builder.toString();
    }

    private static String getSyntax(String syntax) {
        return syntax.replace("public", "<span style=\"color:Blue;\">public</span>")
                     .replace("private", "<span style=\"color:Blue;\">private</span>")
                     .replace("class", "<span style=\"color:Blue;\">class</span>")
                     .replace("extends", "<span style=\"color:Blue;\">extends</span>")
                     .replace("implements", "<span style=\"color:Blue;\">implements</span>");
    }

    private static String getAuthors(List<String> ars) {
        String authors = "";

        for(String a: ars) {
            authors += a + ", ";
        }
        if(!authors.isEmpty()) {
            authors = authors.substring(0, authors.length()-2);
        }

        return authors;
    }

    private static String getTree(Container.Hierarchy hierarchy, String path) {
        if(hierarchy == null) {
            return "";
        }
        StringBuilder tree = new StringBuilder("<a href=\"").append(path).append(hierarchy.className.replace(".", File.separator)).append("\">").append(hierarchy.className).append("</a><br>");
        int i = 1;

        Container.Hierarchy t = hierarchy.next;
        if(t != null) {
            while (t.next != null) {
                add2NBSP(tree, i++);
                tree.append("<a href=\"").append(path).append(t.className.replace(".", File.separator)).append("\">").append(t.className).append("</a><br>");
                t = t.next;
            }
            add2NBSP(tree, i);
            tree.append("<span class=\"selflink\">").append(t.className).append("</span><br>");
        }

        return tree.toString();
    }

    private static void add2NBSP(StringBuilder nbsp, int n) {
        for(int i = 0; i < n; i++) {
            nbsp.append("&nbsp;&nbsp;");
        }
    }

    private static String getPath(String path) {
        StringBuilder sb = new StringBuilder();
        int i = path.indexOf('.');
        while (i != -1) {
            sb.append("../");
            i = path.indexOf('.', i+1);
        }
        sb.append("../");

        return sb.toString();
    }

    private final static String BLOCK = "block";
    private final static String NONE  = "none";

    private final static String PUB_MET   = "<td><img id=\"pubmethod\" alt=\"Public method\" src=\"{path}clear.gif\" title=\"Public method\" class=\"cl_pub_m\"></td>";
    private final static String PRIV_MET  = "<td><img id=\"privmethod\" alt=\"Private method\" src=\"{path}clear.gif\" title=\"Private method\" class=\"cl_priv_m\"></td>";
    private final static String PROT_MET  = "<td><img id=\"protmethod\" alt=\"Protected method\" src=\"{path}clear.gif\" title=\"Protected method\" class=\"cl_prot_m\"></td>";

    private final static String PUB_F   = "<td><img id=\"pubfield\" alt=\"Public field\" src=\"{path}clear.gif\" title=\"Public field\" class=\"cl_pub_f\"></td>";
    private final static String PRIV_F  = "<td><img id=\"privfield\" alt=\"Purivate field\" src=\"{path}clear.gif\" title=\"Private field\" class=\"cl_priv_f\"></td>";
    private final static String PROT_F  = "<td><img id=\"protfield\" alt=\"Protected field\" src=\"{path}clear.gif\" title=\"Protected field\" class=\"cl_prot_f\"></td>";
}